﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using RG.Common.Provider;
using RG.Consts;
using RealtorGuide.Classes;

namespace RG.Classes.Impl.Beta
{
    public class MicroAreaFakeService : IMicroAreaService
    {
        public MicroArea Get(int id)
        {
            return ClassesContainer.MicroAreas.FirstOrDefault(x => x.Id == id);
        }


        public MicroArea Put(MicroArea item)
        {
            if (item.Id != 0)
            {
                ClassesContainer.MicroAreas.Remove(ClassesContainer.MicroAreas.FirstOrDefault(x => x.Id == item.Id));
                ClassesContainer.MicroAreas.Add(item);
                return item;
            }

            item.Id = ++ClassesContainer.MicroAreaGenerator;
            ClassesContainer.MicroAreas.Add(item);
            return item;
        }


        public List<MicroArea> List(MicroAreasQueryParams param)
        {
            return ReturnFiltered(ClassesContainer.MicroAreas, param);
        }


        public List<int> ListPrepare(MicroAreasQueryParams param)
        {
            return ReturnFiltered(ClassesContainer.MicroAreas, param).Select(x => x.Id).ToList();
        }


        private List<MicroArea> ReturnFiltered(List<MicroArea> source, MicroAreasQueryParams param)
        {
            var toReturn = source;

            if (param.Ids != null && param.Ids.Any())
                toReturn = toReturn.Where(x => param.Ids.Contains(x.Id)).ToList();

            return toReturn;
        }

    }
}
